package com.dog.framework.core.model;

import com.dog.framework.core.enums.ResponseCodeEnum;
import lombok.Data;

import java.io.Serializable;
import java.util.Objects;

@Data
public class ResponseResult<T>  implements Serializable {

    private static final long serialVersionUID = 1L;

    private boolean success;

    private Integer code;

    private String msg;

    private long timestamp;

    private T data;


    private ResponseResult() {
        this.timestamp = System.currentTimeMillis();
    }

    public ResponseResult(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }


    public static <T> ResponseResult<T> success(String message) {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setCode(ResponseCodeEnum.SUCCESS.getCode());
        responseResult.setSuccess(true);
        responseResult.setMsg(message);
        return responseResult;
    }

    public static <T> ResponseResult<T> success() {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setCode(ResponseCodeEnum.SUCCESS.getCode());
        responseResult.setSuccess(true);
        responseResult.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
        return responseResult;
    }

    public static <T> ResponseResult<T> success(T data) {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setCode(ResponseCodeEnum.SUCCESS.getCode());
        responseResult.setSuccess(true);
        responseResult.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
        responseResult.setData(data);
        return responseResult;
    }

    public static <T> ResponseResult<T> fail(String message) {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setCode(ResponseCodeEnum.INTERNAL_SERVER_ERROR.getCode());
        responseResult.setSuccess(false);
        responseResult.setMsg(message);
        return responseResult;
    }

    public static <T> ResponseResult<T> fail(Integer code, String message) {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setCode(code);
        responseResult.setSuccess(false);
        responseResult.setMsg(message);
        return responseResult;
    }


    public static <T> ResponseResult<T> fail(ResponseCodeEnum errorCodeEnum) {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setCode(errorCodeEnum.getCode());
        responseResult.setSuccess(false);
        responseResult.setMsg(errorCodeEnum.getMsg());
        return responseResult;
    }



    public static <T> ResponseResult<T> fail() {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setCode(ResponseCodeEnum.INTERNAL_SERVER_ERROR.getCode());
        responseResult.setSuccess(false);
        responseResult.setMsg(ResponseCodeEnum.INTERNAL_SERVER_ERROR.getMsg());
        return responseResult;
    }


    public static boolean isSuccess(ResponseResult<?> responseResult) {
        return Objects.nonNull(responseResult) && responseResult.getCode().equals(ResponseCodeEnum.SUCCESS.getCode());
    }


    public static <T> ResponseResult<T> judge(boolean status) {
        if (status) {
            return success();
        } else {
            return fail();
        }
    }


}
